package khmt.lvtn.dhbk.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import khmt.lvtn.dhbk.summary.RetrieveOntology;

import khmt.lvtn.dhbk.model.SentimentModel;
import khmt.lvtn.dhbk.model.process.MainProcess;
import khmt.lvtn.dhbk.model.process.PR;
import khmt.lvtn.dhbk.process.Run;
import khmt.lvtn.dhbk.comment.Comment;
import khmt.lvtn.dhbk.comment.Result;

public class Gui extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int scale = 2;
	private JLabel commentLabel;
	private JLabel resultLabel;
	private JPanel panel;
	private JPanel mainPanel;
	private JPanel mainPanel2;
	private JPanel mainPanel3;
	private JRadioButton radioMenuForText;
	private JRadioButton radioMenuForFile;
	private ButtonGroup bg;
	private JPanel northOfSouthPanel;
	private JPanel centreOfSouthPanel;
	private JButton runButton = new JButton("Run");
	private JButton refreshButton = new JButton("Back");
	private JButton start = new JButton("Start");
	private JButton home = new JButton("Home");
	private JPanel panelForHoemForFile = new JPanel();
	private JButton homeForFile = new JButton("Home");
	private JScrollPane commentScrollPane;
	private JScrollPane resultScrollPane;
	private JTextArea commentTextArea;
	private JTextArea resultTextArea;
	private int frameSize;
	private int frameHigh;
	private JPanel northPanel;
	private JPanel southPanel;
	private JLabel runTimeLabel;
	private String time;
	private String result;
	private JPanel ceterPanelOfMainPanel3;
	private JButton chooseFile = new JButton("FileChooser");
	private String nameFileChoice = "/data";
	private JTextField inputTextFiels = new JTextField(20);
	private JTextField outputTextField = new JTextField(20);
	private JLabel inputLabel = new JLabel("InputFile");
	private JLabel outputLabel = new JLabel("OutputFile");
	private JPanel southPanelOfMainPanel3;
	private JButton processFileButton = new JButton("Run");
	private String runningText = "<html><font color=blue size=3>" + "<b>"
			+ "                              			Running..." + "</b>"
			+ "</font>" + "</html>";
	private JLabel processLabel = new JLabel(runningText);
	private String positiveOrNegativeResultPath = Run.PositiveOrNegativeResultForCommentsPath;
	private JPanel panelForSouthOfMainPanel3;
	private JLabel timeLabel;
	private JLabel precisionLabel;
	private JLabel recallLabel;

	private JButton backForMain3 = new JButton("Back");
	private JPanel runHomeForComment = new JPanel();

	public Gui() {
		// processFileButton.setBackground(Color.black);
		processLabel.setText(runningText);
		panel = new JPanel();
		this.add(panel);
		result = "Nothing";
		time = "";
		mainPanel = new JPanel();
		mainPanel.setVisible(false);
		panel.add(mainPanel);
		mainPanel2 = new JPanel();
		mainPanel3 = new JPanel();
		mainPanel3.setVisible(false);
		panel.add(mainPanel3);
		panel.add(mainPanel2);
		bg = new ButtonGroup();
		radioMenuForText = new JRadioButton("Comment");		
		radioMenuForFile = new JRadioButton("File");
		bg.add(radioMenuForFile);
		bg.add(radioMenuForText);
		radioMenuForText.setSelected(true);
		northPanel = new JPanel();
		southPanel = new JPanel();
		northOfSouthPanel = new JPanel();
		centreOfSouthPanel = new JPanel();
		commentTextArea = new JTextArea(7, 60);		
		Font fComment=new Font("Times New Roman", Font.BOLD, 10*scale);
		commentTextArea.setFont(fComment);
		commentTextArea.setLineWrap(true);
		commentTextArea.setWrapStyleWord(true);
		resultTextArea = new JTextArea(result, 7, 60);
		resultTextArea.setFont(fComment);
		resultTextArea.setEditable(false);
		commentScrollPane = new JScrollPane(commentTextArea);
	
		commentLabel = new JLabel("Comment");
		Font commonFont= new Font("Times New Roman", Font.ITALIC, 18);
		commentLabel.setFont(commonFont);

		resultLabel = new JLabel("     Result");
		resultLabel.setFont(commonFont);
		runTimeLabel = new JLabel(time);
		resultScrollPane = new JScrollPane(resultTextArea);
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(northPanel, BorderLayout.NORTH);
		northPanel.add(commentLabel);
		northPanel.add(commentScrollPane);

		mainPanel.add(runHomeForComment, BorderLayout.CENTER);
		// mainPanel.add(runButton, BorderLayout.CENTER);
		// mainPanel.add(home,BorderLayout.EAST);
		runHomeForComment.add(runButton);
		runHomeForComment.add(home);
		mainPanel.add(southPanel, BorderLayout.SOUTH);
		southPanel.setLayout(new BorderLayout());

		southPanel.add(northOfSouthPanel, BorderLayout.NORTH);
		southPanel.add(centreOfSouthPanel, BorderLayout.CENTER);
		northOfSouthPanel.add(resultLabel);
		northOfSouthPanel.add(resultScrollPane);
		centreOfSouthPanel.add(refreshButton);
		// centreOfSouthPanel.add(home);
		centreOfSouthPanel.add(runTimeLabel);

		southPanel.setVisible(false);

		mainPanel2.add(radioMenuForText);
		mainPanel2.add(radioMenuForFile);
		mainPanel2.add(start);
		// mainPanel3
		ceterPanelOfMainPanel3 = new JPanel();
		mainPanel3.setLayout(new BorderLayout());
		// panelForHoemForFile.add(homeForFile);
		mainPanel3.add(panelForHoemForFile, BorderLayout.NORTH);
		ceterPanelOfMainPanel3.add(chooseFile);
		ceterPanelOfMainPanel3.add(inputLabel);
		ceterPanelOfMainPanel3.add(inputTextFiels);
		ceterPanelOfMainPanel3.add(outputLabel);
		ceterPanelOfMainPanel3.add(outputTextField);
		mainPanel3.add(ceterPanelOfMainPanel3, BorderLayout.CENTER);
		inputTextFiels.setText(Run.commentsPath);
		outputTextField.setText(Run.resultForCommentsPath);

		southPanelOfMainPanel3 = new JPanel();
		mainPanel3.add(southPanelOfMainPanel3, BorderLayout.SOUTH);
		// southPanelOfMainPanel3.setLayout(new BorderLayout());
		southPanelOfMainPanel3.add(processFileButton);
		southPanelOfMainPanel3.add(homeForFile);

		// southPanelOfMainPanel3.add(processLabel);
		processLabel.setVisible(false);
		panelForSouthOfMainPanel3 = new JPanel();
		timeLabel = new JLabel();
		precisionLabel = new JLabel("100%");
		recallLabel = new JLabel("100%");
		southPanelOfMainPanel3.add(panelForSouthOfMainPanel3,
				BorderLayout.SOUTH);
		panelForSouthOfMainPanel3.add(timeLabel);
		panelForSouthOfMainPanel3.add(precisionLabel);
		panelForSouthOfMainPanel3.add(recallLabel);
		panelForSouthOfMainPanel3.add(backForMain3);
		panelForSouthOfMainPanel3.setVisible(false);
		buttonWorking();
		setTitle("VietNamese Sentiment");
		setResizable(false);
		setSize((setFrameSize(400)), (setFrameHigh(100)));
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(400, 200);
		setAlwaysOnTop(true);
	}

	private void buttonWorking() {
		// TODO Auto-generated method stub
		runWorking();
		refreshWorking();
		startWorking();
		homeWorking();
		homeForFileWorking();
		processForFileWorking();
		chooseFileWorking();
		backForMainWorking();
	}

	private void backForMainWorking() {
		// TODO Auto-generated method stub
		backForMain3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				inputTextFiels.setEditable(true);
				outputTextField.setEditable(true);
				processLabel.setVisible(false);
				processFileButton.setEnabled(true);
				homeForFile.setEnabled(true);
				setSize(800, 130);
				panelForSouthOfMainPanel3.setVisible(false);
				processFileButton.setVisible(true);
				homeForFile.setVisible(true);
				chooseFile.setVisible(true);
			}
		});
	}

	private void chooseFileWorking() {
		// TODO Auto-generated method stub
		chooseFile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JFileChooser fchooser = new JFileChooser("input");
				positiveOrNegativeResultPath = Run.PositiveOrNegativeResultForCommentsPath;
				inputTextFiels.setText(nameFileChoice);
				if (fchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					nameFileChoice = fchooser.getSelectedFile().getPath();
					inputTextFiels.setText(nameFileChoice);
					positiveOrNegativeResultPath = Run.PositiveOrNegativeResultForCommentsPath;
				}
			}
		});
	}

	private void processForFileWorking() {
		// TODO Auto-generated method stub
		processFileButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String input = inputTextFiels.getText();
				String output = outputTextField.getText();
				processLabel.setVisible(true);

				if (input != null && output != null) {
					if (input.compareTo("") != 0 && output.compareTo("") != 0) {
						long time = 0;
						homeForFile.setEnabled(true);
						if (input.equals("")) {
							input = Run.commentsPath;
						}

						// processFileButton.setText("Running");
						// processFileButton.setVisible(false);
						// if(processFileButton.getText().equals("Running")){

						time = Run.processForCommentsFile(input, output,
								positiveOrNegativeResultPath);
						// }

						setSize(800, 130);
						processFileButton.setEnabled(true);

						if (time > 0) {
							String str1 = "<html><font color=blue size=10>"
									+ "<b>" + "" + "</b>" + "</font>"
									+ "</html>";
							processLabel.setText(str1);
							// String str2 = "<html><font color=#9932CC size=3>"
							// + "<b>" +
							// "Time: " +Long.toString(time)+ "</b>" + "</font>"
							// + "</html>";
							String str2 = "<html><font color=#666362 size=3>"
									+ "<b>" + "Time: " + Long.toString(time)
									+ "</b>" + "</font>" + "</html>";
							timeLabel.setText(str2);
							MainProcess mainProcess = new MainProcess();
							String p = "";
							String r = "";
							PR pr = mainProcess
									.getPR(Run.realResult,
											Run.PositiveOrNegativeResultForCommentsPath);
							p = Float.toString(pr.getP());
							r = Float.toString(pr.getR());
							precisionLabel
									.setText("<html><font color=black size=3>"
											+ "	P=" + p + "	" + "</html>");
							recallLabel
									.setText("<html><font color=black size=3>"
											+ "	R=" + r + "	" + "</html>");
							panelForSouthOfMainPanel3.setVisible(true);
							processFileButton.setVisible(false);
							homeForFile.setVisible(false);
							chooseFile.setVisible(false);
						}

					}
				}
			}
		});
	}

	private void homeForFileWorking() {
		// TODO Auto-generated method stub
		this.homeForFile.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				setSize(400, 100);
				mainPanel3.setSize(800, 200);
				processFileButton.setVisible(true);
				processLabel.setVisible(false);
				mainPanel3.setVisible(false);
				mainPanel2.setVisible(true);
			}
		});
	}

	private void homeWorking() {
		// TODO Auto-generated method stub
		this.home.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				setSize(400, 100);
				resultTextArea.setText("");
				southPanel.setVisible(false);
				mainPanel.setVisible(false);
				mainPanel2.setVisible(true);
				runHomeForComment.add(home);
			}
		});
	}

	private void startWorking() {
		// TODO Auto-generated method stub
		this.start.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (radioMenuForText.isSelected()) {
					runButton.setVisible(true);
					southPanel.setVisible(false);
					commentTextArea.setEditable(true);
					setSize(500*scale, 150*scale);
					setLocation(200,200);
					mainPanel.setVisible(true);
					mainPanel2.setVisible(false);
				} else {
					setSize(800, 130);
					mainPanel2.setVisible(false);
					mainPanel3.setVisible(true);
					homeForFile.setVisible(true);
				}
			}
		});
	}

	// for comment panel
	private void runWorking() {
		// TODO Auto-generated method stub
		this.runButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				RetrieveOntology retrOntology = RetrieveOntology.getInstant();
				retrOntology.readOntology(Run.entityPath, Run.propertyPath,
						Run.adjPath, Run.verbPath, Run.auxiPath,
						Run.negativePath, Run.pronoun);
				southPanel.setVisible(!southPanel.isVisible());
				setSize(500*scale, 237*scale);
				setLocation(200, 200);			
				runButton.setVisible(false);
				commentTextArea.setEditable(false);
				String com = commentTextArea.getText();
				Comment comment = null;
				comment = new Comment(com);
				Result result = comment.getResult();
				if (result.getOneOrMultiEntity() == 1) {
					String Entity = "";
					String sentiment = "NEUTRAL";
					if (comment.getSuperEntiTextList().size() == 1) {
						Entity = comment.getSuperEntiTextList().get(0);
					}
					if (result.getSentimentForOneCase() > 0) {
						sentiment = "POSITIVE";
					} else if (result.getSentimentForOneCase() < 0) {
						sentiment = "NEGATIVE";
					}
					if (!Entity.equals("")) {
						resultTextArea.setText(Entity + ": " + sentiment);
					} else {
						resultTextArea.setText(sentiment);
					}

				} else if (result.getOneOrMultiEntity() == 2) {
					ArrayList<SentimentModel> resultForMultiEntity = result
							.getSentimentForMultCase();
					String reslt = "";
					for (int i = 0; i < resultForMultiEntity.size(); i++) {
						String sentiment = "NEUTRAL";
						float score = resultForMultiEntity.get(i).getScore();
						if (score > 0) {
							sentiment = "POSITIVE";
						} else if (score < 0) {
							sentiment = "NEGATIVE";
						}

						reslt += resultForMultiEntity.get(i).getWordSentiment()
								+ ": " + sentiment + "\n";

					}
					resultTextArea.setText(reslt);
				}
				centreOfSouthPanel.add(home);
				Run.deleteTempFile();
			}

		});
	}

	public void refreshWorking() {
		// TODO Auto-generated method stub
		this.refreshButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				commentTextArea.setText("");
				result = "";
				resultTextArea.setText(result);
				setSize(500*scale, 150*scale);
				setLocation(200, 200);
				runButton.setVisible(true);
				southPanel.setVisible(!southPanel.isVisible());
				commentTextArea.setEditable(true);
				runHomeForComment.add(home);
			}
		});
	}

	public int setFrameSize(int frameSize) {
		this.frameSize = frameSize;
		return frameSize;
	}

	public int getFrameSize() {
		return frameSize;
	}

	public int setFrameHigh(int frameHigh) {
		this.frameHigh = frameHigh;
		return frameHigh;
	}

	public int getFrameHigh() {
		return frameHigh;
	}
}